package com.comprental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comprental.dto.PageBean;
import com.comprental.entity.LeasingCredit;
import com.comprental.service.LeasingCreditService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Objects;
import java.util.Optional;

/**
 * @Auther: jzhang
 * @Date: 2019/4/6 22:58
 * @Description: 后台技术文章控制器
 */
@Controller
@AllArgsConstructor
@RequestMapping("/user/credit")
public class CreditUserController {
    private LeasingCreditService creditService;
    /**
     * 进入技术文章列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String index(Model model, LeasingCredit credit, PageBean pageBean) {
        //查询所有文字记录
        Integer pageIndex = Optional.ofNullable(pageBean.getPage()).orElse(1);
        Integer pageSize = Optional.ofNullable(pageBean.getSize()).orElse(10);

        QueryWrapper queryWrapper = new QueryWrapper();

        if (Objects.nonNull(credit)) {
            if (!StringUtils.isEmpty(credit.getOrderId())) {
                queryWrapper.eq("orderId", credit.getOrderId());
                model.addAttribute("orderId", credit.getOrderId());
            }
            if (!StringUtils.isEmpty(credit.getMobile())) {
                queryWrapper.eq("mobile", credit.getMobile());
                model.addAttribute("mobile", credit.getMobile());
            }
        }
        queryWrapper.orderByDesc("createDate");

        IPage<LeasingCredit> creditPage = creditService.page(new Page<>(pageIndex,pageSize), queryWrapper);
        model.addAttribute("creditList", creditPage.getRecords());
        model.addAttribute("count", creditPage.getTotal());
        model.addAttribute("curr", creditPage.getCurrent());

        return "creditList";
    }
}
